/*Fichier traitant de la géolocalisation*/

/*Variables globales*/
var map = null;

var init_lat = 47.223182304127;
var init_lng = 2.0661163330078;
var initLocation = null;
var userLocation = null;

var Geocoder = null;

var userMarker;

var posLoaded = Array();

/*Fonctions*/
function initializeMap(lat, lng) {
    if(lat == null || lng == null)
        initLocation = new google.maps.LatLng(init_lat, init_lng);
    else
    {
        userLocation = new google.maps.LatLng(lat, lng);
        initLocation = userLocation;
    }

    var myOptions = {
        zoom: 6,
        center: initLocation,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("div_map"),
        myOptions);

    map.setMapTypeId(google.maps.MapTypeId.ROADMAP);

    if(lat != null && lng != null)
        placeUserMarker();

}

function geoloc_ip()
{
    //localisation par Google
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            userLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
            getAddressFromLatLng(userLocation);
        }, function() {
            handleNoGeolocation();
        });
    }	

    function handleNoGeolocation() {
        userLocation = null;
        userLocation = new google.maps.LatLng(geoip_latitude(), geoip_longitude());
        getAddressFromLatLng(userLocation);
    }
	

}

function getAddressFromLatLng(userLocation)
{
    var address = document.getElementById("geocode_input").value;
    gmap_createGeocoder();

    if (Geocoder) {
        Geocoder.geocode( {
            'location': userLocation
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                userLocation = results[0].geometry.location;
                var add = results[0].formatted_address;
                location.href="geolocalisation.php?lat="+userLocation.lat()+"&lng="+userLocation.lng()+"&address="+add;
            } 
            else 
            {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });
    }
}


function geoloc_adresse()
{
    var address = document.getElementById("geocode_input").value;
    if(address != '')
    {
        gmap_createGeocoder();

        if (Geocoder) {
            Geocoder.geocode( {
                'address': address
            }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    userLocation = results[0].geometry.location;
                    location.href="geolocalisation.php?lat="+userLocation.lat()+"&lng="+userLocation.lng()+"&address="+address;
                } 
                else 
                {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });

        }
    }
}

function gmap_createGeocoder()
{
    if (Geocoder == null)
    {
        Geocoder = new google.maps.Geocoder();
    }
}

function placeUserMarker()
{
    if (userMarker != null)
    {
        userMarker.setPosition(userLocation);
    }
    else
    {
        var options = {
            title: "Utilisateur",
            draggable: true,
            clickable: true,
            position : userLocation,
            map : map
        };
        userMarker = new google.maps.Marker(options);
        google.maps.event.addListener(userMarker, 'dragend', endDragHandler);
        google.maps.event.addListener(userMarker, 'click', function() {openInfoTab(userLocation.lat(), userLocation.lng(), "(il y a un instant)", "telle adresse", userMarker)});
        map.setZoom(13);
    }
}

function endDragHandler()
{
    userLocation = userMarker.getPosition();
    getAddressFromLatLng(userLocation);
}

function savePosition()
{
    var address = document.getElementById("geocode_input").value;
    location.href="geolocalisation.php?lat="+userLocation.lat()+"&lng="+userLocation.lng()+"&address="+address+"&action=SAVE";
}

function getAllPositions()
{
    var address = document.getElementById("geocode_input").value;
    var dest = "geolocalisation.php?action=LOAD_ALL";
    if(address != '')
        dest += "&address="+address;
    if(userLocation != null)
        dest += "&lat="+userLocation.lat()+"&lng="+userLocation.lng();
    
    location.href = dest;
}

function showPosition(indice, lat, lng, address, date)
{
    if(posLoaded[indice] == null)
    {
        var options = {
            title: "Utilisateur ("+date+")",
            draggable: false,
            clickable: true,
            position : new google.maps.LatLng(lat, lng),
            map : map
        };
        posLoaded[indice] = new google.maps.Marker(options);
        google.maps.event.addListener(posLoaded[indice], 'click', function(){openInfoTab(lat, lng, date, address, posLoaded[indice])});
    }
    //else
        openInfoTab(lat, lng, date, address, posLoaded[indice]);
}

function openInfoTab(lat, lng, date, address, marker)
{
    var div = "<div>";
    div += "<p>Position enregistrée le "+date+"<br />";
    div += "à telle adresse : <br />"+address+"</p>";
    div += "</div>";
    
    var infowindow = new google.maps.InfoWindow({
        content: div
    });
    
    map.setCenter(new google.maps.LatLng(lat, lng));
    infowindow.open(map,marker);
}
